package com.shut.vehicle.controller;

import com.shut.vehicle.controller.result.Result;
import com.shut.vehicle.interceptor.token.Back;
import com.shut.vehicle.interceptor.token.Client;
import com.shut.vehicle.interceptor.token.PassToken;
import com.shut.vehicle.interceptor.token.UserThread;
import com.shut.vehicle.pojo.User;
import com.shut.vehicle.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author 卢斌
 */
@CrossOrigin
@ResponseBody
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录与注册
     */
    @PassToken
    @GetMapping(value = "/login")
    public Result userLogin(@RequestParam("code") String code) {
        String userToken = userService.userLogin(code);
        return Result.ok(userToken);
    }

    @Client
    @GetMapping(value = "/self_info")
    public Result selfUserInfo() {
        Integer userId = UserThread.userId();
        User user = userService.getSelfInfo(userId);
        return Result.ok(user);
    }


    @Back
    @GetMapping(value = "/search_user")
    public Result searchUser(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        List<User> userList = userService.getAllUsers(pageNum, pageSize);
        return Result.ok(userList);
    }


    @PassToken
    @GetMapping(value = "/test")
    public Result testConnect() {
        return Result.ok();
    }



}
